//
//  ViewController.m
//  DK_TestApp
//
//  Created by DK on 17/1/9.
//  Copyright © 2017年 ChunYu. All rights reserved.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <SceneKit/SceneKit.h>
#import "Father.h"
#import "NSArray+arrayExtention.h"

#import "NSDictionary+SafeNil.h"

@interface ViewController ()<WKScriptMessageHandler,WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, weak)  UIScrollView *videoListScrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //            Class cls = self.class;
    //            Ivar label = class_getInstanceVariable(cls, "_goodAtLabel");
    //            if (label != NULL) {
    //               _goodLabel =  object_getIvar(self, label);
    //                _goodLabel.numberOfLines = 2;
    //                _goodLabel.textColor = HexRGB(0x999999);
    //            }
    
    NSString *str = [NSNull null];
    BOOL isEque = [str isEqualToString:@"oooo"];
    
    NSString *a = nil;
    NSDictionary *dic = @{@"aaaa":a,@"bbb":@"cccc"};
    NSArray *array = @[@"1",@"2"];
    
    NSString *aoo = array[4];
    
    NSMutableDictionary *md = [NSMutableDictionary dictionary];
    [md setObject:nil forKey:@"aaaa"];
    NSLog(@"%@",md);

    [md setObject:nil forKeyedSubscript:nil];// 此处object标记为nullable
    NSLog(@"----%p,%p--%p,%p--%p,%p--%p,%p",dic.superclass,[dic class],object_getClassName(dic));
    
    NSLog(@"%@",md);
    
    NSString *aNull = dic[@"aaaa"];
   bool cNull =  [aNull isEqualToString:@"cccc"];
    
    NSLog(@"%@",cNull);
    NSNull *nullbbbb = [NSNull null];
    
    [nullbbbb performSelector:@selector(abcdefg) withObject:nil];
}

- (BOOL)abcdefg{

    NSLog(@"2222");
    return YES;

}

- (UIImage*) gradientImageFromColors:(NSArray*)colors withFrame: (CGRect)frame{
    
    NSMutableArray *ar = [NSMutableArray array];
   
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    
    UIGraphicsBeginImageContextWithOptions(frame.size, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    
    CGPoint start = CGPointMake(0.0,0.0);
    CGPoint end = CGPointMake(0.0, frame.size.height);
    
    CGContextDrawLinearGradient(context, gradient, start, end,kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//    CGContextClearRect(context, frame);

    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    
    return image;
}

- (void)click:(UIButton *)sender{
    
    sender.selected = YES;
    NSLog(@"-----%@",sender);
    [self.videoListScrollView setContentOffset:CGPointMake(sender.tag *100, 0) animated:YES];


}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    NSLog(@"%@",message.body);
    
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

}
//加载完成时调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    [webView evaluateJavaScript:[NSString stringWithFormat:@"document.images.length"] completionHandler:^(id _Nullable response, NSError * _Nullable error) {
        
        if (response != 0) {
            
            for (int i=0; i<[response intValue]; i++) {
                [webView evaluateJavaScript:[NSString stringWithFormat:@"document.images[%d].style.maxWidth='100%%'",i] completionHandler:^(id _Nullable response, NSError * _Nullable error) {
                    //                    NSLog(@"response1: %@ error: %@", response, error);
                }];
                [webView evaluateJavaScript:[NSString stringWithFormat:@"document.images[%d].style.height='auto'",i] completionHandler:^(id _Nullable response, NSError * _Nullable error) {
                    //                    NSLog(@"response2: %@ error: %@", response, error);
                }];
            }
            
        }
        //        NSLog(@"response0: %@ error: %@", response, error);
    }];
    
    [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust = '60%'" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
        //        NSLog(@"response3: %@ error: %@", response, error);
    }];
    
}
#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    
}

// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    
}

// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation {
}


 // 接收到服务器跳转请求之后调用
 - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
 
 }
 
 // 在收到响应后，决定是否跳转
 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
     NSLog(@"----");
 }
 
// 在发送请求之前，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
}



@end
